package com.roshan.domain;

import org.springframework.transaction.annotation.Transactional;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.persistence.Version;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Configurable
public class Rsvp {

    @NotNull
    @Size(min = 1, max = 30)
    private String code;

    @Size(max = 30)
    private String email;

    private Integer attending;

    @Size(max = 100)
    private String specialRequests;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date confirmed;

    @PersistenceContext
    transient EntityManager entityManager;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return this.version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Rsvp attached = Rsvp.findRsvp(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public Rsvp merge() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        Rsvp merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public static final EntityManager entityManager() {
        EntityManager em = new Rsvp().entityManager;
        if (em == null)
            throw new IllegalStateException(
                    "Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countRsvps() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Rsvp o", Long.class).getSingleResult();
    }

    public static List<Rsvp> findAllRsvps() {
        return entityManager().createQuery("SELECT o FROM Rsvp o", Rsvp.class).getResultList();
    }

    public static Rsvp findRsvp(Long id) {
        if (id == null)
            return null;
        return entityManager().find(Rsvp.class, id);
    }

    public static List<Rsvp> findRsvpEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Rsvp o", Rsvp.class).setFirstResult(firstResult)
                .setMaxResults(maxResults).getResultList();
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getAttending() {
        return this.attending;
    }

    public void setAttending(Integer attending) {
        this.attending = attending;
    }

    public String getSpecialRequests() {
        return this.specialRequests;
    }

    public void setSpecialRequests(String specialRequests) {
        this.specialRequests = specialRequests;
    }

    public Date getConfirmed() {
        return this.confirmed;
    }

    public void setConfirmed(Date confirmed) {
        this.confirmed = confirmed;
    }

    public static TypedQuery<Rsvp> findRsvpsByCodeEquals(String code) {
        if (code == null || code.length() == 0)
            throw new IllegalArgumentException("The code argument is required");
        EntityManager em = Rsvp.entityManager();
        TypedQuery<Rsvp> q = em.createQuery("SELECT o FROM Rsvp AS o WHERE o.code = :code", Rsvp.class);
        q.setParameter("code", code);
        return q;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Attending: ").append(getAttending()).append(", ");
        sb.append("Code: ").append(getCode()).append(", ");
        sb.append("Confirmed: ").append(getConfirmed()).append(", ");
        sb.append("Email: ").append(getEmail()).append(", ");
        sb.append("Id: ").append(getId()).append(", ");
        sb.append("SpecialRequests: ").append(getSpecialRequests()).append(", ");
        sb.append("Version: ").append(getVersion());
        return sb.toString();
    }
}
